import constants from '@/constants'

const utils = {
	//去空格
	trim: function(value) {
		return value.replace(/(^\s*)|(\s*$)/g, "");
	},
	//内容替换
	replaceAll: function(text, repstr, newstr) {
		return text.replace(new RegExp(repstr, "gm"), newstr);
	},
	//格式化手机号码
	formatNumber: function(num) {
		return num.length === 11 ? num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2') : num;
	},
	//金额格式化
	rmoney: function(money) {
		return parseFloat(money).toFixed(2).toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,').replace(
			/\,$/, '').split('').reverse().join('');
	},
	//日期格式化
	formatDate: function(formatStr, fdate) {
		if (fdate) {
			if (~fdate.indexOf('.')) {
				fdate = fdate.substring(0, fdate.indexOf('.'));
			}
			fdate = fdate.toString().replace('T', ' ').replace(/\-/g, '/');
			var fTime, fStr = 'ymdhis';
			if (!formatStr)
				formatStr = "y-m-d h:i:s";
			if (fdate)
				fTime = new Date(fdate);
			else
				fTime = new Date();
			var month = fTime.getMonth() + 1;
			var day = fTime.getDate();
			var hours = fTime.getHours();
			var minu = fTime.getMinutes();
			var second = fTime.getSeconds();
			month = month < 10 ? '0' + month : month;
			day = day < 10 ? '0' + day : day;
			hours = hours < 10 ? ('0' + hours) : hours;
			minu = minu < 10 ? '0' + minu : minu;
			second = second < 10 ? '0' + second : second;
			var formatArr = [
				fTime.getFullYear().toString(),
				month.toString(),
				day.toString(),
				hours.toString(),
				minu.toString(),
				second.toString()
			]
			for (var i = 0; i < formatArr.length; i++) {
				formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
			}
			return formatStr;
		} else {
			return "";
		}
    },
    
    /**
     * @description: 获取收藏列表
     * @param {type} 
     * @return: 
     */
    GetCollectList: function(){
        let COLLECTLIST = uni.getStorageSync(constants.COLLECTLIST)
        if( !Array.isArray(COLLECTLIST) ){
            COLLECTLIST = []
        }
        return COLLECTLIST
    },

    /**
     * @description: 添加收藏列表
     * @param {type} 
     * @return: 
     */
    addCollectList: function(goods){
        let COLLECTLIST = this.GetCollectList()
        if( !Array.isArray(COLLECTLIST) ){
            COLLECTLIST = []
        }
        
        const { productCode } = goods
        let tempList = COLLECTLIST.filter(item => item.productCode === productCode)
    
        return new Promise((resolve, reject) => {
            if(tempList.length > 0){
                reject(COLLECTLIST)
            }
            COLLECTLIST.push(goods)
            uni.setStorageSync(constants.COLLECTLIST, COLLECTLIST)
            resolve(COLLECTLIST)
        })
    
    },


    /**
     * @description: 移除收藏列表
     * @param {type} 
     * @return: 
     */
    removeCollectList: function(goods){
        let COLLECTLIST = this.GetCollectList()
        if( !Array.isArray(COLLECTLIST) ){
            return
        }

        const { productCode } = goods
        let tempList = COLLECTLIST.filter(item => item.productCode !== productCode)
        uni.setStorageSync(constants.COLLECTLIST, tempList)
        return
    },
	
	/**
	 * @description 券是否在有效期内
	 * @param {Object} date1
	 * @param {Object} date2
	 */
	expired(date1, date2) {
		if (!date1 || !date2) {
			return true
		}
		const dateS = new Date(date1.replace('T', ' ').replace(/-/g, '/')).getTime()
		const dateE = new Date(date2.replace('T', ' ').replace(/-/g, '/')).getTime()
		const now = Date.now()
		return now < dateS || now > dateE
	}


}

module.exports = {
	trim: utils.trim,
	replaceAll: utils.replaceAll,
	formatNumber: utils.formatNumber,
	rmoney: utils.rmoney,
    formatDate: utils.formatDate,
    GetCollectList: utils.GetCollectList,
    addCollectList: utils.addCollectList,
    removeCollectList: utils.removeCollectList,
	expired: utils.expired,
}
